import json

from django.http import HttpResponse, JsonResponse
from rest_framework.decorators import api_view
from TestModel import models
from django.core import serializers


@api_view(['GET'])
def getRouteList(request, experiment_id):
    routeList = models.RouteDetail.objects.filter(experimentId=experiment_id)
    result = json.loads(serializers.serialize("json", routeList))
    # print(result)
    routeList = []
    for res in result:
        routeList_temp = res["fields"]
        routeList_temp["id"] = res["pk"]
        if res["fields"]["status"] == 1:
            routeList_temp["show"] = True
        else:
            routeList_temp["show"] = False
        routeList.append(routeList_temp)
    res_json = {
        "errorCode": 200,
        "data": {
            "configs": routeList,
        }
    }
    return JsonResponse(res_json, safe=False)


@api_view(['POST'])
def saveRouteConfig(request):
    body = json.loads(request.body.decode('utf-8'))
    id = body["experimentId"]
    internalIdMax = body["internalIdMax"]
    configs = body["configs"]
    for config in configs:
        if('id' in config.keys()):
            models.RouteDetail.objects.filter(id=config["id"]).update(color=config["color"],
                                                                      protoType=config["protoType"],
                                                                      # intervals=config["intervals"],
                                                                      # packageSize=config["packageSize"],
                                                                      sourceNodeId=config["sourceNodeId"],
                                                                      sourceNodeName=config["sourceNodeName"],
                                                                      targetNodeId=config["targetNodeId"],
                                                                      targetNodeName=config["targetNodeName"],
                                                                   )
        else:
            if (config["show"]):
                status = 1
            else:
                status = 0
            models.RouteDetail.objects.create(experimentId=id,
                                              color=config["color"],
                                              intervals=config["interval"],
                                              packageSize=config["package"],
                                              protoType=config["protoType"],
                                              sourceNodeId=config["sourceNodeId"],
                                              sourceNodeName=config["sourceNodeName"],
                                              targetNodeId=config["targetNodeId"],
                                              targetNodeName=config["targetNodeName"],
                                              status=status,
                                              selected=0,
                                              )
    models.Experiment.objects.filter(id=id).update(internalIdMax=internalIdMax)
    result = {
        "errorCode": 200
    }
    return JsonResponse(result, safe=False)
@api_view(['POST'])
def deleteRouteConfig(request):
    body = json.loads(request.body.decode('utf-8'))
    routeListId = body["configs"][0]
    experimentId = body["experimentId"]
    models.RouteDetail.objects.filter(id=routeListId).delete()
    result = {
        "errorCode": 200
    }
    return JsonResponse(result, safe=False)
